package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/20 20:23
 * @description：https://leetcode-cn.com/problems/heaters/
 */
public class 供暖器 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.findRadius(new int[]{1, 5}, new int[]{1, 2}));
    }

    class Solution {
        public int findRadius(int[] houses, int[] heaters) {
            int house = 0;
            int heater = 0;
            int r = heaters[0] - houses[0];
            while (house < houses.length) {
                int curHeater = heaters[heater];
                while (house < houses.length && houses[house] <= curHeater + r) house++;
                if (++heater == heaters.length) break;
                if (Math.abs(heaters[heater] - houses[house]) > r) {
                    r = (int) Math.ceil((double) (Math.abs(heaters[heater] - houses[house]) + r) / 2);
                }
            }
            if (house == houses.length) return r;
            else {
                r = houses[house] + r >= houses[houses.length - 1] ? r : houses[houses.length - 1] - houses[house];
                return r;
            }
        }
    }
}
